package com.xiaoxi.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxi.yygh.common.result.Result;
import com.xiaoxi.yygh.common.utils.MD5;
import com.xiaoxi.yygh.hosp.service.HospitalSetService;
import com.xiaoxi.yygh.model.hosp.HospitalSet;
import com.xiaoxi.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@CrossOrigin
public class HospitalSetController {
    
//    127.0.0.1:8201/admin/hosp/hospitalSet/findAll
    @Autowired
    private HospitalSetService hospitalSetService;
    
    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("/findAll")
    public Result findAllHospitalSet(){
        List<HospitalSet> list = hospitalSetService.list();
        
        return Result.ok(list);
    }
    
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("/{id}")
    public Result removeHospSet(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        return flag ? Result.ok() : Result.fail();
    }
    
    // 3.条件查询带分页
    @ApiOperation(value = "条件查询带分页")
    @PostMapping("/findPageHospSet/{currentPage}/{pageSize}")
    public Result findPageHospSet(@PathVariable long currentPage,
                                  @PathVariable long pageSize,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        IPage<HospitalSet> page = new Page<>(currentPage, pageSize);

        LambdaQueryWrapper<HospitalSet> lqw = new LambdaQueryWrapper<>();
        lqw.like(hospitalSetQueryVo.getHosname() != null, HospitalSet::getHosname, hospitalSetQueryVo.getHosname());
        lqw.eq(hospitalSetQueryVo.getHoscode() != null, HospitalSet::getHoscode, hospitalSetQueryVo.getHoscode());
        lqw.orderByDesc(HospitalSet::getUpdateTime);
        
        hospitalSetService.page(page, lqw);
        
        return Result.ok(page);
    }
     
    // 4.添加医院设置
    @ApiOperation(value = "添加医院设置")
    @PostMapping("/saveHospSet")
    public Result saveHospSet(@RequestBody HospitalSet hospitalSet){
        // 设置状态 1可用 0不能使用
        hospitalSet.setStatus(1);
        
        // 设置签名秘钥 signKey
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() +"" + random.nextInt(1000)));

        boolean save = hospitalSetService.save(hospitalSet);
        
        return save ? Result.ok() : Result.fail();
    }
    
    // 5.根据id获取医院设置
    @ApiOperation(value = "根据id获取医院设置")
    @GetMapping("/getHospset/{id}")
    public Result getHospSet(@PathVariable long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        
        return Result.ok(hospitalSet);
    }
    
    // 6.修改医院设置
    @ApiOperation(value = "修改医院设置")
    @PostMapping("/updateHospSet")
    public Result updateHospSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        return flag ? Result.ok() : Result.fail();
    }
    
    // 7.批量删除医院设置
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids){
        hospitalSetService.removeByIds(ids);
        
        return Result.ok();
    }
    
    // 8.医院设置锁定和解锁
    @ApiOperation(value = "医院设置锁定和解锁")
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status){
        // 1.根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        // 2.设置状态
        hospitalSet.setStatus(status);
        // 3.修改
        hospitalSetService.updateById(hospitalSet);
        
        return Result.ok();
    }
    
    // 9.发送签名秘钥
    @ApiOperation(value = "发送签名秘钥")
    @PutMapping("/sendKey/{id}")
    public Result sendKey(@PathVariable Long id){

        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        
        // TODO 发送短信
        return Result.ok();
    }
}
